home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / cbibcode.arc / QSORT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-05  |  1.0 KB  |  29 lines

  1. /* qsort on page 319 of the Turbo C Bible */
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. int mycompare(const void *, const void *);
  6. main(int argc, char **argv, char **envp)
  7. {
  8.    unsigned int i, count;
  9.    char **p_table, **result;
  10.                /* Find length of encironment table and print it */
  11.    printf("==== Unsorted environment table ====\n");
  12.    for(count = 0, p_table = envp; *p_table != NULL; p_table++, count++)
  13.                         printf("%s\n", *p_table);
  14.         /* sort the environment table using "qsort" */
  15.    qsotr((void *) envp, (size_t)count, (size_t)sizeof(char *), mycompare);
  16.                        /* Print sorted environment table */
  17.    printf("===== Sorted environment table =====\n");
  18.    for(i = 0, p_table = envp; i< count; i++)
  19.    {
  20.       printf("%s\n", *p_table);
  21.       p_table++;
  22.    }
  23. }
  24.                        /* ----------------------------- */
  25. int mycompare(const void *arg1, const void *arg2)
  26. {
  27.            /* Compare two strings up to the length of the key  */
  28.    return(strncmp(*(char**)arg1, *(char**)arg2, strlen(*(char**)arg1)));
  29. }